/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktemplate;

import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author 210504
 */
public class Enemy1 extends Collideable implements Renderable, Updateable {

    static ArrayList<Enemy1> all = new ArrayList<Enemy1>();
    static final double MOVE_SPEED_CARDINAL = 2;
    static final int MOVE = 0;
    static final int ATTACK = 1;
    static final int NULL = 2;
    static final int MAX_HEALTH = 100;
    int state = NULL;
    Image sprite;
    float x, y, xprev, yprev;
    Stack pointStack = new Stack();
    int direction = 0;
    Attackable target = null;
    int targetX, targetY;
    boolean lastPressed = false;
    private int health = MAX_HEALTH;
    private int damage = 1;
    private int steps = 0;
    float dx, dy;
    boolean poison = false;
    Enemy1(float x, float y) {
        all.add(this);
        this.x = MathHelper.align_to_grid_point(x);
        this.y = MathHelper.align_to_grid_point(y);

        try {
            sprite = new Image("sprites/enemy01.png"); //try to load the sprite
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
    }

    @Override
    public void render(Graphics g) {
        sprite.setRotation((float)(Math.atan2(dy, dx)*180/Math.PI));
        sprite.draw((float) x + 1, (float) y);
        g.setColor(Color.red);
        g.fillRect(x, y, sprite.getWidth(), 6);
        g.setColor(Color.green);
        g.fillRect(x, y, sprite.getWidth()*health/MAX_HEALTH, 6);
    }

    @Override
    public boolean update(Input input, StateBasedGame sb) {
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);

        if (state == MOVE) {
            dx = (float)(Math.signum(targetX - x) * MOVE_SPEED_CARDINAL);
            dy = (float)(Math.signum(targetY - y) * MOVE_SPEED_CARDINAL);
            x += dx;
            y += dy;
        } else if(state == ATTACK) {
            target.subHealth(damage);
        }
        if(poison){
            subHealth(0.1F);
            steps++;
        }
        if(steps >= 1000){
            poison = false;
        }


        if (health <= 0) {//Killed by something
            Game.money += 25;
            return true;
        }
        
        return false;


    }

    public void setTargetPoint(int x, int y) {
        targetX = x;
        targetY = y;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public Attackable getNearestTurret(ArrayList<ArrayList> turrets) {
            Attackable closest = null;
            double closestDistance = Double.POSITIVE_INFINITY;
            for(ArrayList<Attackable> list : turrets) {
                for(Attackable current : list) {
                    if(closest == null || (MathHelper.point_distance(x, y, current.getX(), current.getY()) < closestDistance)) {
                        closest = current;
                        closestDistance = MathHelper.point_distance(x, y, current.getX(), current.getY());
                    }
                }
            }
            return closest;

    }

    public boolean checkAdjacent(Wall target) {
        if (MathHelper.point_distance(target.x, target.y, x, y) < 32) {
            return true;
        } else {
            return false;
        }
    }

    public float getHealth() {
        return health;
    }

    public void subHealth(float down) {
        health -= down;
    }

    public void findPathAroundObstacle(Object obstacle) {
    }

    public void mousePressed(int button) {
    }
    public void setSpeed(int nspeed){

    }
    public void setPoison(boolean setto){
        if(poison&&setto){
            steps = 0;
        }
        poison = setto;
    }
}
